package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmel = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmel);
        setmealMapper.save(setmel);
        Long setmealId = setmel.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                        setmealDish.setSetmealId(setmealId);
                    }
            );
            setmealDishMapper.save(setmealDishes);

        }


    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        Integer have = setmealMapper.isHave();
        if(have<=0){
            throw new BaseException("没有东西可删了...");
        }
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            if(setmeal.getStatus()==1){
                throw new BaseException("套餐正在销售中...");
            }
            setmealMapper.deleteBatch(id);
            setmealDishMapper.deleteBatch(id);
        }

    }

    @Override
    public void starOrStop(Integer status, Long id) {
        setmealMapper.starOrStop(status,id);
    }

    @Override
    public SetmealVO selectWithDishById(Long id) {
        //根据套餐id查询套餐类对象
        Setmeal setmeal = setmealMapper.selectById(id);
        //根据套餐id查询出所有对应的菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
        //将查询到的套餐类和对应的套餐中的菜品集合封装到返回给前端的类
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;

    }

    @Override
    public void update(SetmealVO setmealVO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmealMapper.update(setmeal);
        setmealMapper.deleteByStemealId(setmealVO.getId());
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        if(setmealDishes!=null&&setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealVO.getId());
            });
            setmealDishMapper.insertBatch(setmealDishes);


        }

    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
